package com.cqfae.deploycentral.app.services.impl;

import com.cqfae.deploycentral.app.base.BaseConstants;
import com.cqfae.deploycentral.app.dao.entity.ReportInfo;
import com.cqfae.deploycentral.app.dao.entity.ReportRequire;
import com.cqfae.deploycentral.app.dao.mapper.ReportRequireMapper;
import com.cqfae.deploycentral.app.services.ReportRequireService;
import com.cqfae.deploycentral.app.utils.ConvertUtil;
import com.cqfae.deploycentral.app.utils.GsonUtils;
import com.cqfae.deploycentral.app.web.request.ReportRequireRequest;
import com.cqfae.deploycentral.app.web.response.IPage;
import com.cqfae.deploycentral.app.web.vo.ReportRequireVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.List;

/**
 * Created by YANGHENG940 on 2017/7/12.
 */
@Service
@Transactional
public class ReportRequireServiceImpl implements ReportRequireService {

    @Autowired
    private ReportRequireMapper reportRequireMapper;

    @Override
    public IPage<ReportRequireVo> page(ReportRequireRequest request)  {
        Page<ReportInfo> page = PageHelper.startPage(request.getPageNum(), request.getPageSize());
        Example example = new Example(ReportRequire.class);
        example.setOrderByClause("CREATE_TIME DESC");
        example.createCriteria().andEqualTo("reportCode",request.getReportCode());
        reportRequireMapper.selectByExample(example);
        IPage<ReportRequireVo> ipage = ConvertUtil.pageToIpage(page, new IPage<ReportRequireVo>());
        List<ReportRequireVo> result = GsonUtils.getInstance().fromJson(GsonUtils.getInstance().toJson(page), new TypeToken<List<ReportRequireVo>>(){}.getType());
        for(ReportRequireVo vo:result){
            vo.setCheckStatusName(BaseConstants.getConstantsCodeMap()
                    .get(vo.getCheckStatus()));
        }
        ipage.setResult(result);
        return ipage;
    }

    @Override
    public List<ReportRequire> pageList(ReportRequireRequest request)  {
        Page<ReportRequire> page = PageHelper.startPage(request.getPageNum(), request.getPageSize());
        Example example = new Example(ReportRequire.class);
        example.setOrderByClause("CREATE_TIME DESC");
        reportRequireMapper.selectByExample(example);
        List<ReportRequire> result = GsonUtils.getInstance().fromJson(GsonUtils.getInstance().toJson(page), new TypeToken<List<ReportRequire>>(){}.getType());
        return result;
    }

    @Override
    public Integer save(ReportRequire entity) {
        return reportRequireMapper.insertSelective(entity);
    }

    @Override
    public Integer saveBatch(List<ReportRequire> entitys) {
        int i = 0 ;
        for(ReportRequire entity : entitys){
            i = i + this.save(entity);
        }
        return i;
    }

    @Override
    public List<ReportRequire> findAll() {
        return reportRequireMapper.selectAll();
    }

    @Override
    public ReportRequire getOne(ReportRequire entity) {
        return reportRequireMapper.selectOne(entity);
    }

    @Override
    public Integer deleteOne(Long id) {
        ReportRequire reportRequire = new ReportRequire();
        reportRequire.setId(id);
        return reportRequireMapper.delete(reportRequire);
    }
}
